from django.shortcuts import render, redirect, reverse
from web.forms.project import ProjectModelForm
from django.http import JsonResponse
from scripts.init_price_police import S28
from scripts.cos_create_bucket import create_bucket
import time

def project_list(request):
    if request.method == 'GET':
        my_project_list = {'true': [], 'my': [], 'join': []}
        my_create_project = S28().py_searche('select * from project where creator_id={0}'.format(request.user_id))
        my_invitation_project = S28().py_searche('select * from project where id=(select project_id from invitation where user_information_id={0})'.format(request.user_id))
        for i in my_create_project:
            if i[4] == 'true':
                #my_project_list['true'].append(i)
                my_project_list['true'].append({'value': i, 'type':'my'})
            else:
                my_project_list['my'].append(i)

        for i in my_invitation_project:
            if i[4] == 'true':
                my_project_list['true'].append({'value': i, 'type': 'join'})
            else:
                my_project_list['join'].append(i)

        form = ProjectModelForm(request)
        return render(request, 'project_list.html', {'form': form, 'my_project_list': my_project_list})
    form = ProjectModelForm(request, data=request.POST)
    if form.is_valid():
        # 验证通过 数据路添加项目
        project_name = form.cleaned_data.get('project_name')
        color = form.cleaned_data.get('color')
        desc = form.cleaned_data.get('desc')
        creator = request.user_id
        print(project_name, color, desc, creator)
        print('把上面的项目上传到数据库中')
        S28().insert_into('project', {'project_name':project_name,'miaoshu': desc,'color': color,'creator_id': creator})

        id = S28().search('project', {'project_name': '{}'.format(project_name)})
        print(id, '----------------------------------------------------id是对山了')
        create_bucket('{}-1302094470'.format(str(id[0][0])))
        return JsonResponse({'status': True, 'data': '创建成功'})
    return JsonResponse({'status': False, 'error': form.errors})


def project_star(request, project_type, project_id):
    """星标项目 可用ajax做"""
    if project_type == 'my':
        S28().py_searche1('update project set sign="true" where id={0} and creator_id={1}'.format(project_id, str(request.user_id)))
        print(request.user_id)
        return redirect('project_list')
    if project_type == 'join':
        S28().py_searche1(
            'update invitation  set sign="true" where project_id="{0}" and user_information_id="{1}"'.format(
                project_id, str(request.user_id)))
        return redirect('project_list')

def project_unstar(request, project_type, project_id):
    if project_type == 'my':
        S28().py_searche1('update project set sign="false" where id={0} and creator_id={1}'.format(project_id, str(request.user_id)))
        print(request.user_id)
        return redirect('project_list')
    if project_type == 'join':
        S28().py_searche1(
            'update invitation  set sign="false" where project_id="{0}" and user_information_id="{1}"'.format(
                project_id, str(request.user_id)))
        return redirect('project_list')